package com.wd.tech.view.activity;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.wd.tech.R;
import com.wd.tech.base.BaseActivity;
import com.wd.tech.base.BasePresenter;
import com.wd.tech.bean.LoginBean;
import com.wd.tech.jiami.RsaCoder;
import com.wd.tech.mvp.IPresenterImpl;
import com.wd.tech.url.BaseUrl;
import com.wd.tech.utils.NetUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

public class RegisterActivity extends BaseActivity {

    @BindView(R.id.et_niceng)
    EditText etNiceng;
    @BindView(R.id.et_regis_phone)
    EditText etRegisPhone;
    @BindView(R.id.et_regins_pwd)
    EditText etReginsPwd;
    @BindView(R.id.tv_register)
    TextView tvRegister;
    @BindView(R.id.bt_login)
    Button btLogin;
    private String zhuchejiamipwd;
    private boolean wang;
    @Override
    protected void initData() {
        //网络判断
        wang = NetUtil.getInstance().HasNet();

        btLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Map<String,Object> map=new HashMap<>();
                String nicheng = etNiceng.getText().toString();
                if (nicheng.isEmpty()){
                    Toast.makeText(RegisterActivity.this, "请输入昵称", Toast.LENGTH_SHORT).show();
                    return;
                }else {
                    map.put("nickName",nicheng);
                }
                String phone = etRegisPhone.getText().toString();
                if (phone.isEmpty()){
                    Toast.makeText(RegisterActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
                    return;
                }else {
                    map.put("phone",phone);
                }
                String pwd=etReginsPwd.getText().toString();
                try {
                    zhuchejiamipwd = RsaCoder.encryptByPublicKey(pwd);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (pwd.isEmpty()){
                    Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }else {
                    map.put("pwd",zhuchejiamipwd);
                }
                mPresenter.IModelRegisterInfo(BaseUrl.RegisterUrl,map, LoginBean.class);
            }
        });
    }

    @Override
    protected void initView() {

    }

    @Override
    protected BasePresenter initPresenter() {
        return new IPresenterImpl();
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_register;
    }

    @Override
    public void success(Object o) {
        if (o instanceof LoginBean){
            if (wang){
                if (o instanceof LoginBean){
                    if (((LoginBean) o).getStatus().equals("0000")){
                        Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
                        return;
                    }else {
                        Toast.makeText(this, ""+((LoginBean) o).getMessage(), Toast.LENGTH_SHORT).show();
                    }

                }else {
                    Toast.makeText(this, "请连接网络~~", Toast.LENGTH_SHORT).show();
                }
            }else {
                Toast.makeText(this, "无网", Toast.LENGTH_SHORT).show();
                return;
            }
        }
    }

    @Override
    public void Error(Throwable throwable) {

    }


}
